/*
var vue = require('vue');
var shared = require('@vue/shared');
var lodashUnified = require('lodash-unified');
// require('element-plus/es/utils/index.mjs');
var position = require('element-plus/es/utils/dom/position.mjs');
var error = require('element-plus/es/utils/error.mjs');
var scroll = require('element-plus/es/utils/dom/scroll.mjs');
*/

import * as vue from 'vue';               // vue 可能需要改为命名空间导入
import * as shared from '@vue/shared';    // 如果报错同上需要调整
import * as lodashUnified from 'lodash-unified';  // 关键修复点
// import 'element-plus/es/utils/index.mjs';
import * as position from 'element-plus/es/utils/dom/position.mjs';
import * as error from 'element-plus/es/utils/error.mjs';
import * as scroll from 'element-plus/es/utils/dom/scroll.mjs';


const SCOPE = "ElInfiniteScroll";
const CHECK_INTERVAL = 50;
const DEFAULT_DELAY = 200;
const DEFAULT_DISTANCE = 0;
const attributes = {
	delay: {
		type: Number,
		default: DEFAULT_DELAY
	},
	distance: {
		type: Number,
		default: DEFAULT_DISTANCE
	},
	disabled: {
		type: Boolean,
		default: false
	},
	immediate: {
		type: Boolean,
		default: true
	}
};
const getScrollOptions = (el, instance) => {
	return Object.entries(attributes).reduce((acm, [name, option]) => {
		var _a, _b;
		const {
			type,
			default: defaultValue
		} = option;
		const attrVal = el.getAttribute(`infinite-scroll-${name}`);
		let value = (_b = (_a = instance[attrVal]) != null ? _a : attrVal) != null ? _b : defaultValue;
		value = value === "false" ? false : value;
		value = type(value);
		acm[name] = Number.isNaN(value) ? defaultValue : value;
		return acm;
	}, {});
};
const destroyObserver = (el) => {
	const {
		observer
	} = el[SCOPE];
	if (observer) {
		observer.disconnect();
		delete el[SCOPE].observer;
	}
};
const handleScroll = (el, cb) => {
	const {
		container,
		containerEl,
		instance,
		observer,
		lastScrollTop
	} = el[SCOPE];
	const {
		disabled,
		distance
	} = getScrollOptions(el, instance);
	const {
		clientHeight,
		scrollHeight,
		scrollTop
	} = containerEl;

	const delta = Math.abs(scrollTop) - lastScrollTop;
	el[SCOPE].lastScrollTop = Math.abs(scrollTop);
	if (observer || disabled || delta < 0)
		return;
	let shouldTrigger = false;
	if (container === el) {
		shouldTrigger = scrollHeight - (clientHeight + Math.abs(scrollTop)) <= distance;
	} else {
		const {
			clientTop,
			scrollHeight: height
		} = el;
		const offsetTop = position.getOffsetTopDistance(el, containerEl);
		shouldTrigger = scrollTop + clientHeight >= offsetTop + clientTop + height - distance;
		console.error("参数2")
	}
	if (shouldTrigger) {
		cb.call(instance);
	}
};

function checkFull(el, cb) {
	const {
		containerEl,
		instance
	} = el[SCOPE];
	const {
		disabled
	} = getScrollOptions(el, instance);
	if (disabled || containerEl.clientHeight === 0)
		return;
	if (containerEl.scrollHeight <= containerEl.clientHeight) {
		cb.call(instance);
	} else {
		destroyObserver(el);
	}
}
const InfiniteScroll = {
	async mounted(el, binding) {
		const {
			instance,
			value: cb
		} = binding;
		if (!shared.isFunction(cb)) {
			error.throwError(SCOPE, "'v-infinite-scroll-v2' binding value must be a function");
		}
		await vue.nextTick();
		const {
			delay,
			immediate
		} = getScrollOptions(el, instance);
		const container = scroll.getScrollContainer(el, true);
		const containerEl = container === window ? document.documentElement : container;
		const onScroll = lodashUnified.throttle(handleScroll.bind(null, el, cb), delay);
		if (!container)
			return;
		el[SCOPE] = {
			instance,
			container,
			containerEl,
			delay,
			cb,
			onScroll,
			lastScrollTop: containerEl.scrollTop
		};
		if (immediate) {
			const observer = new MutationObserver(lodashUnified.throttle(checkFull.bind(null, el, cb),
				CHECK_INTERVAL));
			el[SCOPE].observer = observer;
			observer.observe(el, {
				childList: true,
				subtree: true
			});
			checkFull(el, cb);
		}
		container.addEventListener("scroll", onScroll);
	},
	unmounted(el) {
		if (!el[SCOPE])
			return;
		const {
			container,
			onScroll
		} = el[SCOPE];
		container == null ? void 0 : container.removeEventListener("scroll", onScroll);
		destroyObserver(el);
	},
	async updated(el) {
		if (!el[SCOPE]) {
			await vue.nextTick();
		} else {
			const {
				containerEl,
				cb,
				observer
			} = el[SCOPE];
			if (containerEl.clientHeight && observer) {
				checkFull(el, cb);
			}
		}
	}
};

export default InfiniteScroll;
